<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form
          ref="userForm"
          label-width="220px"
          :model="formParmas"
          :rules="rules"
        >
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="formParmas.username"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="formParmas.workNumber"
                  :disabled="!!this.$route.query.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="formParmas.mobile"
                 
                  :disabled="!!this.$route.query.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <treeData
                  class="inputW"
                  v-model="formParmas.departmentId"
                ></treeData>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select
                  size="mini"
                  class="inputW"
                  v-model="formParmas.formOfEmployment"
                >
                  <el-option label="正式" :value="1"></el-option>
                  <el-option label="非正式" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formParmas.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formParmas.correctionTime"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <uplode v-model="formParmas.staffPhoto"></uplode>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button size="mini" type="primary" @click="submitForm"
                >保存更新</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
//引入组件
import treeData from "./components/treeData.vue";
import uplode from "./components/uplode.vue";
//引入接口
import { addOrUpdateUser, getUserDetail } from "@/api/user";
export default {
  name: "detail",
  data() {
    return {
      formParmas: {
        username: "",
        mobile: "",
        formOfEmployment: 1,
        workNumber: "",
        departmentId: 0,
        timeOfEntry: "",
        correctionTime: "",
        staffPhoto: "",
      },
      //规则
      rules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "姓名为1-4位",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        departmentId: [
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (this.formParmas.timeOfEntry) {
                if (new Date(this.formParmas.timeOfEntry) > new Date(value)) {
                  callback(new Error("转正时间不能小于入职时间"));
                  return;
                }
              }
              callback();
            },
          },
        ],
      },
    };
  },
  mounted() {
    this.getUserDetail();
  },
  methods: {
    //保存按钮的回调
    async submitForm() {
      if (this.$route.query.id) {
      
          this.$set(this.formParmas,"id",this.$route.query.id)
       
         await addOrUpdateUser(this.formParmas);
        //提示
        this.$message({
          type: "success",
          message: "修改成功",
        });
        //路由跳转
        this.$router.push({
          path: "/employee",
        });
      } else {
        //调用表单校验方法
        await this.$refs.userForm.validate();
        //调用接口
        await addOrUpdateUser(this.formParmas);
        //提示
        this.$message({
          type: "success",
          message: "添加成功",
        });
        //路由跳转
        this.$router.push({
          path: "/employee",
        });
      }
    },
    //获取用户详细的信息
    async getUserDetail() {
      if (this.$route.query.id) {
        const id = this.$route.query.id;
        //调用接口
        let reslut = await getUserDetail(id);
        //合并数据
        Object.assign(this.formParmas, reslut);
      } else return;
    },
  },
  //注册组件
  components: {
    treeData,
    uplode
  },
};
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
